<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="common/common_header :: common_header('部门管理')"></head>
<body class="childrenBody">
<blockquote class="layui-elem-quote quoteBox">
    <form class="layui-form" method="post" id="searchFrm">
        <div class="layui-form-item" style="text-align: center;">
            <div class="layui-inline">
                <label class="layui-form-label">部门名称:</label>
                <div class="layui-input-inline">
                    <input type="text" name="title" placeholder="请输入部门名称" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">部门地址</label>
                <div class="layui-input-inline">
                    <input type="text" name="address" placeholder="请输入部门地址" autocomplete="off" class="layui-input">

                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">部门备注</label>
                <div class="layui-input-inline">
                    <input type="text" name="remark" placeholder="请输入部门备注" autocomplete="off" class="layui-input">

                </div>
            </div>
            <div class="layui-inline">
                <button type="button" class="layui-btn" id="doSearch"><span class='layui-icon'>&#xe615;</span>搜索
                </button>
                <button type="reset" class="layui-btn layui-btn-danger"><span class='layui-icon'>&#xe669;</span>重置
                </button>
            </div>
        </div>
    </form>
</blockquote>
<table id="deptTable" lay-filter="deptTable"></table>
<!--表头-->
<div id="deptTableToolBar" style="display: none;">
    <a class="layui-btn layui-btn-sm" lay-event="add"><span class="layui-icon">&#xe61f;</span>添加</a>

</div>
<!--操作-->
<div id="deptTableRowBar" style="display: none">
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="update" >编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>

</div>

<!-- 添加和修改的弹出层开始 -->
<div style="display: none;padding: 3px" id="saveOrUpdateDiv">
    <form id="dataFrm" lay-filter="dataFrm" class="layui-form layui-form-pane">
        <div class="layui-form-item">
            <label class="layui-form-label">父级部门</label>
            <div class="layui-input-block">
                <input type="hidden" name="id"/>
                <ul id="deptTree" class="dtree" data-id="0"><font color="blue"></font></ul>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">部门名称</label>
            <div class="layui-input-block">
                <input name="title" id="title" class="layui-input"></input>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">部门地址</label>
            <div class="layui-input-block">
                <input name="address" id="address" class="layui-input"></input>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">部门备注</label>
            <div class="layui-input-block">
                <textarea name="remark" id="remark" class="layui-input"></textarea>
            </div>
        </div>
        <!--        一行可以放两个文本框-->
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">是否展开</label>
                <div class="layui-input-inline">
                    <input type="radio" name="open" value="1" title="展开">
                    <input type="radio" name="open" value="0" title="不展开" checked="checked">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">是否可用</label>
                <div class="layui-input-inline">
                    <input type="radio" name="available" value="1" title="可用" checked="checked">
                    <input type="radio" name="available" value="0" title="不可用">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">排序码:</label>
            <div class="layui-input-block">
                <textarea name="ordernum" id="ordernum" class="layui-input"></textarea>
            </div>
        </div>

        <div class="layui-form-item" style="text-align: center;">
            <button type="button" class="layui-btn" lay-filter="doSubmit" lay-submit=""><span class='layui-icon'>&#xe609;</span>提交
            </button>
            <button type="reset" class="layui-btn layui-btn-danger"><span class='layui-icon'>&#xe669;</span>重置</button>
        </div>
    </form>
</div>
<!-- 添加和修改的弹出层结束 -->
<div th:replace="common/common_foot::common_foot(~{::script})"></div>
<script type="text/javascript">

    var tableIns;
    var $;
    //加上dtree的声明
    layui.extend({
        dtree: '/layui_ext/dtree/dtree'   // {/}的意思即代表采用自有路径，即不跟随 base 路径
    }).use(['table', 'form', 'layer', 'laydate', 'jquery', 'dtree'], function () {
        var table = layui.table;
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;
        $ = layui.jquery;
        var dtree = layui.dtree;


        //渲染数据表格
        tableIns = table.render({
            elem: '#deptTable'
            , url: '/dept/loadAllDept'
            , toolbar: '#deptTableToolBar' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                , layEvent: 'LAYTABLE_TIPS'
                , icon: 'layui-icon-tips'
            }]
            , title: '部门数据表'
            , height: 'full-150'
            , cols: [
                [
                    {type: 'checkbox', fixed: 'left'}
                    , {field: 'id', title: 'ID', align: 'center', fixed: 'left', unresize: true, sort: true}
                    , {field: 'title', title: '部门名称', align: 'center'}
                    , {field: 'address', title: '部门地址', align: 'center'}
                    , {field: 'remark', title: '部门备注', align: 'center'}
                    , {
                    field: 'open', title: '是否展开', align: 'center', templet: function (d) {
                        return d.open == 1 ? "<span color='blue'>展开</span>" : "<span color='red'>不展开</span>"

                    }
                }
                    , {fixed: 'right', title: '操作', toolbar: '#deptTableRowBar', align: 'center'}
                ]
            ]
            , page: true
        });
        //模糊查询
        $("#doSearch").click(function () {
            var params = $("#searchFrm").serialize();
            tableIns.reload({
                url: '/dept/loadAllDept?' + params
            })
        });
        //监听表头按钮的事件
        table.on('toolbar(deptTable)', function (obj) {
            switch (obj.event) {
                case 'add':
                    openAddLayer();
                    break;
            }
        });

        //监听行按钮的事件
        table.on('tool(deptTable)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            if (layEvent === 'update') { //修改
                openUpdateLayer(data);

                // } else if (layEvent === 'view') {
                //
                //     showDept(data.id);

            } else if (layEvent === 'del') { //删除

                deleteDept(data);

            } else if (layEvent === 'view') { //查看
                viewDept(data);
            }
        });

        function deleteDept(data) {
            //先判断有无子节点，有，提示；无，直接删
            $.get("/dept/checkCurrentDeptHasChildren", {id: data.id}, function (res) {
                if (res.data > 0) {
                    layer.msg("当前" + data.title + "有子部门，请先删除子部门");
                } else {
                    layer.confirm('真的删除' + data.title + "吗", function (index) {
                        layer.close(index);
                        //向服务端发送删除指令
                        $.delete("/dept/deleteDept", {id: data.id}, function (obj) {
                            if (obj.code == 200) {
                                tableIns.reload();//重载表格
                                //刷新左边的部门树
                                window.parent.left.leftDeptTree.reload();
                            }
                            layer.msg(obj.msg);
                        })
                    });
                }
            });
        }

        // function showDept(id){
        //
        //     var mainIndex = layer.open({
        //         type: 1,
        //         title: '查看公告',
        //         skin: 'layui-layer-molv', //加上边框
        //         area: ['800px', '520px'], //宽高
        //         content: $("#deskViewDeptDiv"),
        //         success:function (index) {
        //             //向后台发请求
        //             $.get("/dept/loadDeptById",{id:id},function (res) {
        //
        //                 var data=res.data;
        //                 $("#view_title").html(data.title);
        //                 $("#view_opername").html(data.opername);
        //                 //view_createtime
        //                 $("#view_createtime").html(data.createtime);
        //                 $("#view_content").html(data.content);
        //
        //             });
        //         }
        //     })
        // };

        var mainIndex;
        var url;

        //下拉树
        var addOrUpdateDeptTree = dtree.renderSelect({
            elem: "#deptTree",
            url: "/dept/loadAllDeptTreeJson",
            dataStyle: "layuiStyle",  //使用layui风格的数据格式
            dataFormat: "list",  //配置data的风格为list
            selectTips: "请选择父级部门",
            selectInputName: {
                nodeId: "pid",
            },
            response: {message: "msg", statusCode: 0}  //修改response中返回数据的定义
        });

        //打开添加的弹出层
        function openAddLayer() {
            mainIndex = layer.open({
                type: 1,
                title: '部门添加',
                skin: 'layui-layer-molv', //加上边框
                area: ['800px', '520px'], //宽高
                content: $("#saveOrUpdateDiv"),
                success: function (index) {
                    url = "/dept/addDept";
                    //清空原来的数据
                    $("#dataFrm")[0].reset();
                    //刷新树 重新加载
                    addOrUpdateDeptTree.refreshTree();
                    //加载最大的排序码
                    $.get("/dept/loadDeptMaxOrderNum", function (obj) {
                        $("#ordernum").val(obj.data);

                    })
                }
            })
        };

        //编辑
        function openUpdateLayer(data) {
            mainIndex = layer.open({
                type: 1,
                title: '部门修改',
                skin: 'layui-layer-molv', //加上边框
                area: ['800px', '520px'], //宽高
                content: $("#saveOrUpdateDiv"),
                success: function (index) {
                    url = "/dept/updateDept";

                    //给表单数据赋值
                    form.val("dataFrm", data);
                    //初始化下拉树
                    dtree.dataInit("deptTree", data.pid + "");
                    //设置选中
                    dtree.selectVal("deptTree");
                }
            })
        }

        //监听提交按钮的事件
        form.on("submit(doSubmit)", function (data) {
            var params = $("#dataFrm").serialize();
            //向后台发出请求
            $.post(url, params, function (obj) {
                if (obj.code == 200) {
                    //刷新表格
                    tableIns.reload();
                    //刷新左边的部门树
                    window.parent.left.leftDeptTree.reload();

                }
                layer.msg(obj.msg);
                layer.close(mainIndex);

            });
        });

        //批量删除
        // function batchDelete(obj) {
        //     var checkStatus = table.checkStatus('deptTable'); //idTest 即为基础参数 id 对应的值
        //     console.log(checkStatus.data) //获取选中行的数据
        //     console.log(checkStatus.data.length) //获取选中行数量，可作为是否有选中行的条件
        //     console.log(checkStatus.isAll) //表格是否全选
        //     if (checkStatus.data.length == 0) {
        //         layer.msg("请选中要删除的数据行");
        //     } else {
        //         layer.confirm('真的删除选中行么?', function (index) {
        //             layer.close(index);
        //             var params = "";
        //             $.each(checkStatus.data, function (i, item) {
        //                 if (i == 0) {
        //                     params += "ids=" + item.id;
        //                 } else {
        //                     params += "&ids=" + item.id;
        //                 }
        //             });
        //             //向服务端发送删除指令
        //             $.post("/dept/batchDeleteDept", params, function (obj) {
        //                 if (obj.code == 200) {
        //                     tableIns.reload();//重载表格
        //                     //刷新左边的部门树
        //                     window.parent.left.leftDeptTree.reload();
        //                 }
        //                 layer.msg(obj.msg);
        //             })
        //         });
        //     }
        // }
    });

    function reloadTable(id) {
        var params = $("#searchFrm").serialize() + "&id=" + id;
        //alert(params);
        tableIns.reload({
            url: '/dept/loadAllDept?' + params,
            page: {

                curr: 1
            }
        })
    }
</script>

</body>
</html>